﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows;
using System.Windows.Documents;

namespace SNSpirit.Documents.Adatpers.Ubb.Internal
{
	/// <summary>
	/// Helper class to write rich document element inrecusively.
	/// </summary>
	internal class RichDocumentWriter
	{
		/// <summary>
		/// Current Text Element.
		/// </summary>
		private DependencyObject _CurrentElement;

		/// <summary>
		/// Element Stack.
		/// </summary>
		private Stack<DependencyObject> _ElementStack;

		/// <summary>
		/// Get current inline container.
		/// </summary>
		/// <returns>Current inline container.</returns>
		private InlineCollection GetInlineContainer()
		{
			if (_CurrentElement is Span)
			{
				return (_CurrentElement as Span).Inlines;
			}
			else if (_CurrentElement is Paragraph)
			{
				return (_CurrentElement as Paragraph).Inlines;
			}
			else
			{
				throw new InvalidOperationException("Current element is not an inline container.");
			}
		}

		private BlockCollection GetBlockContainer()
		{
			if (_CurrentElement is Section)
			{
				return (_CurrentElement as Section).Blocks;
			}
			else if (_CurrentElement is ListItem)
			{
				return (_CurrentElement as ListItem).Blocks;
			}
			else if (_CurrentElement is TableCell)
			{
				return (_CurrentElement as TableCell).Blocks;
			}
			else if (_CurrentElement is FlowDocument)
			{
				return (_CurrentElement as FlowDocument).Blocks;
			}
			else
			{
				throw new InvalidOperationException("Current element is not an block container.");
			}
		}

		public void WriteElement(DependencyObject element)
		{
			if (element is Inline)
			{
				this.GetInlineContainer().Add(element as Inline);
			}
			else if (element is Block)
			{
				this.GetBlockContainer().Add(element as Block);
			}
			else
			{
				throw new InvalidOperationException("The element to adding cannot be added to the current element.");
			}
		}

		/// <summary>
		/// 进入当前元素（如果元素可进入）。
		/// </summary>
		public void EnterElement()
		{
		}

		/// <summary>
		/// End current element.
		/// </summary>
		public void EndElement()
		{
		}
	}
}
